Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Switch to using feature for RequestServices #369

Closed
wants to merge 5 commits into from
Closed

Switch to using feature for RequestServices #369

wants to merge 5 commits into from

Conversation

HaoK
Copy link
Member

@HaoK HaoK commented Sep 23, 2015

No description provided.

@Tratcher
Copy link
Member

Why? Bug #?

@davidfowl
Copy link
Member

@Tratcher Performance. Making this lazy. The only problem is the fact that IServiceProviders feature is pretty hidden.

private IServiceScope _scope;
private bool _requestServicesSet;

public RequestServicesFeature(IServiceProvider applicationServices, IServiceScopeFactory scopeFactory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need to resolve the factory this early?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve it from AppServices in the getter instead?

@HaoK
Copy link
Member Author

HaoK commented Sep 23, 2015

Added a test to verify we no-op if there's an existing ISPsFeature. Removed IServiceScope from ctors and just GetRequiredService when needed


public void Dispose()
{
if (_scope != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was gonna say this is so old school:

_scope?.Dispose();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can just nuke the if then

{
if (!_requestServicesSet)
{
_scope = ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be defensive since ApplicationServices is settable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks like it. Do we want to throw an exception if you try to set it to null, or just treat that as no-op and basically do nothing and leave RequestServices null if you set ApplicationServices to null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is totally undefined. To repro this you would have to set ApplicationServices to null then before ever touching RequestServices (I just know somebody is going to do something stupid and report a bug 😄). This works today because nothing is lazy. We could store the original application services and use that as a fallback but that might be perplexing. Throwing might be the most reasonable thing. Null isn't bad either so lets flip a coin. If you did this as a customer an exception might be the best since you'd want to know why instead of getting a null ref. On the other hand if you get RequestServices and then set ApplicationServices to null, should that throw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any real scenario where we want to allow them to set ApplicationServices to null? If not, I can just un auto the property and throw for null sets... Won't we fall over all over the place if ApplicationServices is null? I don't think we are checking anywhere in our code today

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why anybody would set it to null unless they were trying to clear it out. Maybe if the old value was null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok well this is a subtle breaking change then, setting to null will throw an exception now... updated and added a test

{
if (value == null)
{
throw new ArgumentNullException(nameof(ApplicationServices));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be value not ApplicationServices afaik

@davidfowl
Copy link
Member

Small change then :shipit: once CI is green

@HaoK
Copy link
Member Author

HaoK commented Sep 23, 2015

49520a2

@HaoK HaoK closed this Sep 23, 2015
@Tratcher Tratcher added this to the 1.0.0-beta8 milestone Sep 23, 2015
@JunTaoLuo JunTaoLuo deleted the haok/9-22sp branch September 20, 2017 17:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants